package com.LeeCode;

/**
 * 字母移位 II
 */

public class Code2381 {
    public static void main(String[] args) {
        String s = "abc";
        int[][] shifts = {{0, 1, 0}, {1, 2, 1}, {0, 2, 1}};
        System.out.println(new Code2381().shiftingLetters(s, shifts));
    }

    public String shiftingLetters(String s, int[][] shifts) {
        int n = s.length();
        int[] diff = new int[n + 1];
        for (int[] shift : shifts) {
            int start = shift[0], end = shift[1], direction = shift[2];
            if (direction == 0) {
                diff[start]--;
                diff[end + 1]++;
            } else {
                diff[start]++;
                diff[end + 1]--;
            }
        }
        char[] chars = s.toCharArray();
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += diff[i];
            int cur = (chars[i] - 'a' + sum) % 26;
            if (cur < 0) cur += 26;
            chars[i] = (char) (cur + 'a');
        }
        return new String(chars);
    }
}
